package com.avic.rong.questionOrAnswer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.ActionBar;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.utils.OnClickLimitListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 *
 */

public class DialogShare {
    private Activity mActivity;
    //    private AlertDialog dialog;
    public UMShareUtil umShareUtil;
    private ShareUtilCallBack shareUtilCallBack;
    private String shareTitle = "";
    private String shareUrl = "";
    private String shareContent = "";
    private String shareImgUrl = "";
    private PopupWindow popupWindow;
    private View view;

    public DialogShare(Activity activity, ShareUtilCallBack callBack) {
        this.mActivity = activity;
        this.shareUtilCallBack = callBack;
        this.shareTitle = mActivity.getResources().getString(R.string.app_name);
        this.shareContent = mActivity.getResources().getString(R.string.app_name);
    }

    public void showDialog(String title, String content, String url, String imgUrl) {
        this.shareTitle = title;
        this.shareContent = content;
        this.shareUrl = url;
        this.shareImgUrl = imgUrl;
      /*  dialog = generateLoginDialog();
        dialog.show();*/
        popupWindow = getPopupWindow();

        try {
            setDialoglayout();
        } catch (Exception e) {
            e.printStackTrace();
        }
        setDialogContentView();
    }

    private AlertDialog generateLoginDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
        return builder.create();
    }

    private PopupWindow getPopupWindow() {
        view = View.inflate(mActivity, R.layout.dialog_share_layout, null);
        popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, false);
        return popupWindow;
    }

    private void setDialoglayout() throws Exception{

      /*  WindowManager windowManager = mActivity.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

        lp.width = (int) (display.getWidth()); // 设置宽度
        lp.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        // lp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        lp.dimAmount = 0.5f;
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        dialog.getWindow().setGravity(Gravity.BOTTOM);
        dialog.getWindow().setAttributes(lp);
        dialog.getWindow().setWindowAnimations(R.style.bottomPopupAnimation);*/



            // 产生背景变暗效果
            WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();

            lp.alpha = 0.4f;
        mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            mActivity.getWindow().setAttributes(lp);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(false);
            popupWindow.setTouchable(true);
            //从下往上弹出动画效果
            popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
            View llRoot = mActivity.getWindow().getDecorView();
            popupWindow.showAtLocation(llRoot, Gravity.BOTTOM, 0, 0);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                // 在dismiss中恢复透明度
                public void onDismiss() {
                    WindowManager.LayoutParams lp = mActivity.getWindow()
                            .getAttributes();
                    lp.alpha = 1f;
                    mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    mActivity.getWindow().setAttributes(lp);
                }
            });


    }

    private void setDialogContentView() {


        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        tvCancel.setOnClickListener(new OnClickLimitListener() {

            @Override
            public void onClickLimit(View v) {
                popWindowDismiss();
            }
        });
        LinearLayout llWx = (LinearLayout) view.findViewById(R.id.ll_wx);
        LinearLayout llWxc = (LinearLayout) view.findViewById(R.id.ll_wxc);
        LinearLayout llWb = (LinearLayout) view.findViewById(R.id.ll_wb);
        LinearLayout llQq = (LinearLayout) view.findViewById(R.id.ll_qq);
        LinearLayout llQqSpace = (LinearLayout) view.findViewById(R.id.ll_qq_space);
        llWx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindowDismiss();
                shareFromPlatfrom(SHARE_MEDIA.WEIXIN);
            }
        });
        llWxc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindowDismiss();
                shareFromPlatfrom(SHARE_MEDIA.WEIXIN_CIRCLE);
            }
        });
        llWb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindowDismiss();
                shareFromPlatfrom(SHARE_MEDIA.SINA);
            }
        });
        llQq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindowDismiss();
                shareFromPlatfrom(SHARE_MEDIA.QQ);
            }
        });
        llQqSpace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindowDismiss();
                shareFromPlatfrom(SHARE_MEDIA.QZONE);
            }
        });


//        dialog.getWindow().setContentView(view);
    }


    /* public void dialogDismiss() {
         if (dialog != null && dialog.isShowing()) {
             dialog.dismiss();
             dialog = null;
         }
     }*/
    public void popWindowDismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

  /*  public boolean isShowing() {
        if (dialog == null)
            return false;
        return dialog.isShowing();
    }*/

    public boolean isShowing() {
        if (popupWindow == null)
            return false;
        return popupWindow.isShowing();
    }

    /***
     * 分享平台
     *
     * @param mShareMedia
     */
    private void shareFromPlatfrom(SHARE_MEDIA mShareMedia) {


        umShareUtil = new UMShareUtil(shareUtilCallBack, mActivity, shareUrl, shareTitle, shareContent, shareImgUrl);
        umShareUtil.shareDialogClick(mShareMedia);

    }

    public void initActivityResultSsoHandler(int requestCode, int resultCode, Intent data) {
        if (umShareUtil != null) {
            umShareUtil.initActivityResultSsoHandler(requestCode, resultCode, data);
        }
    }

}
